package com.lry.basic.algorithm.common;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/12/16
 */
public class KClosest {
    private class KClosestComparator implements Comparator<int[]>{
        @Override
        public int compare(int[] o1, int[] o2) {
            int order1 = o1[0]*o1[0]+o1[1]*o1[1];
            int order2 = o2[0]*o2[0]+o2[1]*o2[1];
            return order1>order2?1:-1;
        }
    }

    public static void main(String[] args) {
        int[][] points = new int[][]{{2,3},{5,-1},{-2,4}};
        new KClosest().kClosest(points,2);
    }

    public int[][] kClosest(int[][] points, int k) {
        if(k>points.length){
            return points;
        }
        Arrays.sort(points,new KClosestComparator());
        int[][] res = new int[k][2];
        for(int i=0;i<k;i++){
            res[i][0] = points[i][0];
            res[i][1] = points[i][1];
        }
        return res;
    }
}
